package com.cet.programmercarl.algorithmperformancanalysis.数组;

/**
 * @program: algorithm
 * @description: 构建螺旋矩阵
 * @author: 陈恩涛
 * @create: 2022-03-10 22:14
 **/
public class LC59 {

    public static void main(String[] args) {
        int[][] result = generateMatrix(4);
        for (int[] ints : result) {
            for (int i : ints) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
    }

    public static int[][] generateMatrix(int n) {

        int top = 0, right = n - 1, bottom = n - 1, left = 0;

        int times = 1;

        int[][] result = new int[n][n];

        while (times <= n * n) {
            for (int i = left; i <= right; i++) {
                result[top][i] = times ++;
            }
            top++;
            for (int i = top; i <= bottom; i++) {
                result[i][right] = times ++;
            }
            right--;
            for (int i = right; i >= left; i--) {
                result[bottom][i] = times++;
            }
            bottom--;
            for (int i = bottom; i >= top; i--) {
                result[i][left] = times++;
            }
            left++;
        }
        return result;
    }
}
